package com.cn.wanxi.servlet.cuisine;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.CuisineModel;
import com.cn.wanxi.service.ICuisineService;
import com.cn.wanxi.service.impl.CuisineServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/back/cuisine/add")
public class CuisineAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1.处理乱码
        resp.setContentType("application/json; charset=utf-8");
//        2.拿到页面传递过来的值
        String name = req.getParameter("name");
        String isShow = req.getParameter("isShow");
//        3.封装model
        CuisineModel cuisineModel = new CuisineModel();
        cuisineModel.setName(name);
        cuisineModel.setIsShow("".equals(isShow) || isShow == null ? 2 : Integer.parseInt(isShow));
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        cuisineModel.setCreateTime(simpleDateFormat.format(new Date()));
//        4.调用服务逻辑层
        ICuisineService iCuisineService = new CuisineServiceImpl();
//        5.得到返回数据
        int count = iCuisineService.add(cuisineModel);
//        6.响应页面
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("count", count);
        resp.getWriter().println(jsonObject);
    }
}
